﻿using System.Diagnostics;

namespace RealidadeAumentada.Entidades
{
    public class EPilhaObjeto<T> : EPilhaPonteiro<T>
    {
        #region Construtor
        protected EPilhaObjeto()
        {
            return;
        }
        #endregion

        #region Obter Instancia
        protected override void ObterInstancia(int i_length)
        {
            base.ObterInstancia(i_length);
            for (int i = 0; i < i_length; i++)
            {
                this._items[i] = CriarElemento();
            }
            return;
        }

        protected void ObterInstancia(int i_length, object i_param)
        {
            base.ObterInstancia(i_length);

            for (int i = 0; i < i_length; i++)
            {
                this._items[i] = CriarElemento(i_param);
            }

            return;
        }
        #endregion

        #region Criar Elemento
        protected virtual T CriarElemento()
        {
            throw new Excessao();
        }

        protected virtual T CriarElemento(object i_param)
        {
            throw new Excessao();
        }
        #endregion

        #region Começa a Forçar
        public T ComecaForcar()
        {
            if (this._length >= this._items.Length)
            {
                return default(T);
            }

            T ret = this._items[this._length];
            this._length++;
            return ret;
        }

        public override T Forcar(T i_object)
        {
            return default(T);
        }
        #endregion

        #region Inicializar
        public void Inicializar(int i_reserv_length)
        {
            if (i_reserv_length >= this._items.Length)
            {
                throw new Excessao();
            }
            this._length = i_reserv_length;
        }
        #endregion

        #region Remover
        public sealed override void Remover(int i_index)
        {
            if (i_index != this._length - 1)
            {
                T item = this._items[i_index];

                base.Remover(i_index);

                this._items[i_index] = item;
            }
            this._length--;
        }
        #endregion

        #region Remover da Lista de Ignorados
        public sealed override void RemoverOrdemIgnorar(int i_index)
        {
            Debug.Assert(this._length > i_index && i_index >= 0);
            if (i_index != this._length - 1)
            {
                T item = this._items[i_index];
                this._items[i_index] = this._items[this._length - 1];
                this._items[this._length - 1] = item;
            }
            this._length--;
        }
        #endregion
    }
}
